/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 * 注意：
 * 本软件为www.tonetn.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.xunyin.modules.goods.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.common.utils.QueryHelpPlus;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.goods.domain.TTeaRoomContainerGoodsLink;
import com.xunyin.modules.goods.service.TTeaRoomContainerGoodsLinkService;
import com.xunyin.modules.goods.service.dto.TTeaRoomContainerGoodsLinkDto;
import com.xunyin.modules.goods.service.dto.TTeaRoomContainerGoodsLinkQueryCriteria;
import com.xunyin.modules.goods.service.mapper.TTeaRoomContainerGoodsLinkMapper;
import com.xunyin.utils.FileUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @author admin
 * @date 2022-10-28
 */
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "tTeaRoomContainerGoodsLink")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TTeaRoomContainerGoodsLinkServiceImpl extends BaseServiceImpl<TTeaRoomContainerGoodsLinkMapper, TTeaRoomContainerGoodsLink> implements TTeaRoomContainerGoodsLinkService {

    private final IGenerator generator;

    private final TTeaRoomContainerGoodsLinkMapper tTeaRoomContainerGoodsLinkMapper;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(TTeaRoomContainerGoodsLinkQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TTeaRoomContainerGoodsLink> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<TTeaRoomContainerGoodsLink> queryAll(TTeaRoomContainerGoodsLinkQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(TTeaRoomContainerGoodsLink.class, criteria));
    }

    @Override
    public List<TTeaRoomContainerGoodsLink> getGoodsOrderRecordByTeaHouseId(Integer teaHouseId) {
        List<TTeaRoomContainerGoodsLink> list = tTeaRoomContainerGoodsLinkMapper.getGoodsOrderRecordByTeaHouseId(teaHouseId);
        return list;
    }

    /**
     * 柜台商品下单后库存自减
     * @return Integer
     */
    @Override
    public Integer reduceStock(Integer id) {
        TTeaRoomContainerGoodsLink tTeaRoomContainerGoodsLink = tTeaRoomContainerGoodsLinkMapper.selectById(id);
        Integer stock = tTeaRoomContainerGoodsLink.getStock();
        if (stock < 1) {
            stock = 0;
        } else {
            stock = stock - 1;
        }
        tTeaRoomContainerGoodsLink.setStock(stock);
        return tTeaRoomContainerGoodsLinkMapper.update(tTeaRoomContainerGoodsLink, Wrappers.<TTeaRoomContainerGoodsLink>lambdaQuery()
                .eq(TTeaRoomContainerGoodsLink::getId, id));
    }


    @Override
    public void download(List<TTeaRoomContainerGoodsLinkDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TTeaRoomContainerGoodsLinkDto tTeaRoomContainerGoodsLink : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("主体ID", tTeaRoomContainerGoodsLink.getMainId());
            map.put("茶室id", tTeaRoomContainerGoodsLink.getTeaHouseId());
            map.put("茶厅id", tTeaRoomContainerGoodsLink.getTeaRoomId());
            map.put("商品id", tTeaRoomContainerGoodsLink.getGoodsId());
            map.put("货柜格子编号", tTeaRoomContainerGoodsLink.getContainerNum());
            map.put("金额", tTeaRoomContainerGoodsLink.getPrice());
            map.put("库存", tTeaRoomContainerGoodsLink.getStock());
            map.put("上下架 0 下架 1 上架", tTeaRoomContainerGoodsLink.getPublishStatus());
            map.put("创建时间", tTeaRoomContainerGoodsLink.getCreateTime());
            map.put("创建人", tTeaRoomContainerGoodsLink.getCreateUserId());
            map.put("修改时间", tTeaRoomContainerGoodsLink.getUpdateTime());
            map.put("修改人", tTeaRoomContainerGoodsLink.getUpdateUserId());
            map.put("状态", tTeaRoomContainerGoodsLink.getStatus());
            map.put(" isDel", tTeaRoomContainerGoodsLink.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
